package com.example.final_project.Usr.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.MainView;
import com.example.final_project.R;
import com.example.final_project.Usr.entry.UserAccount;

public class LoginActivity extends AppCompatActivity {

    private EditText account_ET, password_ET;
    private Button ok_BT, cancel_BT;
    private CheckBox checked;
    private Context context;
    private ImageView user_head;
    private TextView user_name,register,explan;
    public final int LOGIN_SUCCESS = 1;
    public final int LOGIN_FAIL = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        //查找控件
        findById();

        //设置取消按钮
        cancel_BT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, MainView.class);
                intent.setAction("backToUserFrag");
                LoginActivity.this.startActivity(intent);
            }
        });
        ok_BT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checked.isChecked()) {
                    Login();
                } else {
                    Toast.makeText(LoginActivity.this, "请阅读并勾选《橙车行隐私政策》", Toast.LENGTH_SHORT).show();
                }

            }
        });
        //注册点击事件
        Register();
    }

    //根据id找各个控件
    public void findById() {
        context = this.getBaseContext();
        account_ET = this.findViewById(R.id.account_ET);
        password_ET = this.findViewById(R.id.password_ET);
        ok_BT = this.findViewById(R.id.login_ok);
        cancel_BT = this.findViewById(R.id.login_cancel);
        checked = this.findViewById(R.id.check_user_chose);
        user_head = findViewById(R.id.user_head);
        user_name = findViewById(R.id.user_name);
        register = findViewById(R.id.login_register_TV);
        explan = findViewById(R.id.check_user_text);
    }

    //根据账号密码进行验证
    public void Login() {
        String account = account_ET.getText().toString();
        String password = password_ET.getText().toString();
        OkHttps.async("http://10.150.11.9:8086/chengcar/login")
                .addUrlPara("account", account)
                .addUrlPara("password", password)
                .bind(this)
                .setOnResList(UserAccount.class, users -> {
                    for (UserAccount userAccount : users) {
                        String name = userAccount.getName();
                        Integer age = userAccount.getAge();
                        String sex = userAccount.getSex();
                        String phone = userAccount.getPhone();
                        String address = userAccount.getAddress();

                        SharedPreferences sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("name",name);
                        editor.putInt("age",age);
                        editor.putString("sex",sex);
                        editor.putString("phone",phone);
                        editor.putString("address",address);
                        editor.putString("account",account);
                        editor.commit();
                        //验证账号密码是否存在
                        validateLogon();
                    }

                })
                .get();
    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //接受子线程请求的数据
            switch (msg.what){
                case LOGIN_SUCCESS:
                    Intent intent = new Intent(LoginActivity.this, MainView.class);
                    intent.setAction("backToUserFrag");
                    LoginActivity.this.startActivity(intent);
                    break;
                case LOGIN_FAIL:
                    Toast.makeText(context,"账号密码错误，请检查输入是否有误！",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    //验证账号密码是否存在
    public void validateLogon(){

        SharedPreferences sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        if (sp != null && !sp.getString("name","").equals("")){
            //有值，说明登录成功
            Message message_success = new Message();
            message_success.what = LOGIN_SUCCESS;
            handler.sendMessage(message_success);
        }else{
            //没有值，登录失败
            Message message_fail = new Message();
            message_fail.what = LOGIN_FAIL;
            handler.sendMessage(message_fail);
        }

    }

    //注册点击事件
    public void Register(){
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

        explan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,ExplanActivity.class);
                startActivity(intent);
            }
        });

    }

}